package com.situ.company.project.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.project.model.ProjectModel;
import com.situ.company.project.service.IProjectService;
import com.situ.company.project.service.impl.ProjectServiceImpl;
import com.situ.company.util.FmtRequest;

@WebServlet("/project")
public class ProjectServlet extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action=req.getParameter("action");
		Object res="";
		switch(action) {
		case "add":
			res=add(req);
			break;
		case "page":
			res=page(req);
			break;
		case "get":
			res=get(req);
			break;
		case "del":
			res=del(req);
			break;
		case "upd":
			res=upd(req);
			break;
		case "updEmp":
			res=updEmp(req);
			break;
		}
		FmtRequest.write(resp.getWriter(), res);
	}

	private Object updEmp(HttpServletRequest req) {
		ProjectModel model=FmtRequest.parseModel(req, ProjectModel.class);
		return service.updateEmp(model);
	}
	private Object upd(HttpServletRequest req) {
		Map<String,String> prop=new HashMap<>();
		prop.put("name", "name");
		prop.put("code", "code");
		prop.put("date", "date");
		ProjectModel model=FmtRequest.parseModel(req, ProjectModel.class,prop);
		return service.update(model);
	}
	private Object del(HttpServletRequest req) {
		ProjectModel model=new ProjectModel(req.getParameter("code"));
		return service.delete(model);
	}
	private Object get(HttpServletRequest req) {
		ProjectModel model=new ProjectModel(req.getParameter("code"));
		return service.selectModel(model);
	}
	private Object page(HttpServletRequest req) {
		//Map必须写，请求中还包含pageLimit，pageIndex会被反射，找不到
				Map<String,String> prop=new HashMap<>();
				prop.put("name", "name");
				prop.put("code", "code");
				ProjectModel model=FmtRequest.parseModel(req, ProjectModel.class,prop);
				String pageIndex=req.getParameter("pageIndex");
				String pageLimit=req.getParameter("pageLimit");
				model.setPageIndex(Integer.parseInt(pageIndex));
				model.setPageLimit(Integer.parseInt(pageLimit));
				model.setPageOn(true);
				List<ProjectModel> list = service.selectList(model);
				Integer count=service.selectCount(model);
				Map<String,Object> map=new HashMap<>();
				map.put("list", list);
				map.put("count", count);
				return map;
	}
	IProjectService service=new ProjectServiceImpl();
	private Object add(HttpServletRequest req) {
		ProjectModel model=FmtRequest.parseModel(req, ProjectModel.class);
		return service.insert(model);
	}
}
